<?php

namespace ms\modules\article\controllers;

use Yii;
use ms\modules\article\models\ArticleInfo;
use ms\modules\article\models\searchs\SearchArticleInfo;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use ms\modules\article\models\ArticleSite;
use common\models\BaseSite;

/**
 * ArticleInfoController implements the CRUD actions for ArticleInfo model.
 */
class ArticleInfoController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all ArticleInfo models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new SearchArticleInfo();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single ArticleInfo model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new ArticleInfo model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
    	$model = new ArticleInfo();
    	if ($model->load(Yii::$app->request->post())) {
    		$model->publish_time = time();
    		if($model->save()) {
    			if(isset($_POST['site_id']) &&$_POST['site_id']!=0) {
    				foreach($_POST['site_id'] as $k=>$v) {
    					$new_model = new ArticleSite();
    					$new_model->article_id = $model->id;
    					$new_model->site_id = $v;
    					$new_model->save();
    				}
    				return $this->redirect(['view', 'id' => $model->id]);
    			}else{
    				return $this->redirect(['view', 'id' => $model->id]);
    			}
    		}
    	} else {
    		$sites = BaseSite::getAllSite();
    		unset($sites[0]);
    		return $this->render('create', [
    				'model' => $model,
    				'sites' => $sites,
    		]);
    	}
    }

    /**
     * Updates an existing ArticleInfo model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post())) {
        	$model->publish_time = time();
        	if($model->save()) {
        		if(isset($_POST['site_id']) &&$_POST['site_id']!=0) {
        			ArticleSite::deleteAll(['article_id'=>$model->id]);
        			foreach($_POST['site_id'] as $k=>$v) {
        				$new_model = new ArticleSite();
        				$new_model->article_id = $model->id;
        				$new_model->site_id = $v;
        				$new_model->save();
        			}
        			return $this->redirect(['view', 'id' => $model->id]);
        		}else{
        			return $this->redirect(['view', 'id' => $model->id]);
        		}
        	}
        } else {
        	$sites = BaseSite::getAllSite();
        	unset($sites[0]);
        	$had_sites = ArticleSite::find()->select("site_id")->where(['article_id'=>$id])->column();
        	return $this->render('update', [
        			'model' => $model,
        			'sites' => $sites,
        			'had_sites'=>$had_sites,
        	]);
        }
    }

    /**
     * Deletes an existing ArticleInfo model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
    	$transaction = Yii::$app->db->beginTransaction();
    	//删除文章对应的站点信息
    	$rs1 = ArticleSite::deleteAll(['article_id'=>$id]);
    	$rs2 = $this->findModel($id)->delete();
    	if($rs1!==false && $rs2!==false) {
    		$transaction->commit();
    		return $this->redirect(['index']);
    	} else {
    		$transaction->rollBack();
    		$this->msg('删除失败！','/article-info/index');
    	}
    }

    /**
     * Finds the ArticleInfo model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ArticleInfo the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ArticleInfo::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
